﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.Model;
using rock.DAL;
using rock.UI.common;
using rock.BLL;

namespace rock.UI.houseinfo
{
    public partial class FormHouseRoomUpdate : Form
    {
        DalHOUSE_DONG dalHOUSE_DONG = new DalHOUSE_DONG();
        DalHOUSE_XIAOQU dalHOUSE_XIAOQU = new DalHOUSE_XIAOQU();
        DalCFG_CODE dalCFG_CODE = new DalCFG_CODE();
        DalHOUSE_OWNER dalHOUSE_OWNER = new DalHOUSE_OWNER();



        ModelHOUSE_ROOM modelHOUSE_ROOM = new ModelHOUSE_ROOM();
        public ModelHOUSE_ROOM _ModelHOUSE_ROOM
        {
            get
            {
                return modelHOUSE_ROOM;
            }
            set
            {
                modelHOUSE_ROOM = value;
            }
        }


        DalHOUSE_ROOM dalHOUSE_ROOM = new DalHOUSE_ROOM();
        public FormHouseRoomUpdate()
        {
            InitializeComponent();
            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;
        }

        private void comboBox1Xiaoqu_SelectedIndexChanged(object sender, EventArgs e)
        {
            int xiaoquSeqid = (int)this.comboBox1Xiaoqu.SelectedValue;

            //排层
            DataSet xiaoquSet = this.dalHOUSE_DONG.GetList("HOUSE_DONG.HOUSE_XIAOQU_SEQID = " + xiaoquSeqid + " ORDER BY HOUSE_DONG.NAME ASC ");
            this.comboBox2Dong.DisplayMember = "DONG_NAME";
            this.comboBox2Dong.ValueMember = "SEQID";
            this.comboBox2Dong.DataSource = xiaoquSet.Tables[0];
            if (this._ModelHOUSE_ROOM.HOUSE_DONG_SEQID != 0)
            {
                this.comboBox2Dong.SelectedValue = this._ModelHOUSE_ROOM.HOUSE_DONG_SEQID;
            }
        }
        private void FormHouseRoomUpdate_Load(object sender, EventArgs e)
        {
            ModelHOUSE_DONG aHOUSE_DONG = null;
            if (this._ModelHOUSE_ROOM.HOUSE_DONG_SEQID != 0)
            {
                aHOUSE_DONG = dalHOUSE_DONG.GetModel(this._ModelHOUSE_ROOM.HOUSE_DONG_SEQID);
            }


            //市场
            DataSet xiaoquSet = this.dalHOUSE_XIAOQU.GetList("");
            this.comboBox1Xiaoqu.DisplayMember = "NAME";
            this.comboBox1Xiaoqu.ValueMember = "SEQID";
            this.comboBox1Xiaoqu.DataSource = xiaoquSet.Tables[0];
            if (this._ModelHOUSE_ROOM.HOUSE_DONG_SEQID != 0 && aHOUSE_DONG!=null)
            {
                this.comboBox1Xiaoqu.SelectedValue = aHOUSE_DONG.HOUSE_XIAOQU_SEQID + "";
               // comboBox1Xiaoqu_SelectedIndexChanged(null, null);
            }


            //户型
            DataSet cfgCodeSet = this.dalCFG_CODE.GetList("TYPE = '户型'");
            this.comboBox4ROOM_TYPE.DisplayMember = "CODE_VALUE";
            this.comboBox4ROOM_TYPE.ValueMember = "CODE_VALUE";
            this.comboBox4ROOM_TYPE.DataSource = cfgCodeSet.Tables[0];
            if (this._ModelHOUSE_ROOM.ROOM_TYPE != null && !this._ModelHOUSE_ROOM.ROOM_TYPE.Equals(""))
            {
                this.comboBox4ROOM_TYPE.Text = this._ModelHOUSE_ROOM.ROOM_TYPE;
                
            }

            //房屋朝向
            DataSet chaoxiangSet = this.dalCFG_CODE.GetList("TYPE = '房屋朝向'");
            this.comboBox3CHAOXIANG.DisplayMember = "CODE_VALUE";
            this.comboBox3CHAOXIANG.ValueMember = "CODE_VALUE";
            this.comboBox3CHAOXIANG.DataSource = chaoxiangSet.Tables[0];
            if (this._ModelHOUSE_ROOM.CHAOXIANG != null && !this._ModelHOUSE_ROOM.CHAOXIANG.Equals(""))
            {
                this.comboBox3CHAOXIANG.Text = this._ModelHOUSE_ROOM.CHAOXIANG;
            }

            

            this.comboBox7STATUS.DataSource = Constant.HOUSE_ROOM_STATUS_LIST;
             this.comboBox7STATUS.DisplayMember = "Name";
             this.comboBox7STATUS.ValueMember = "Id";
            if (this._ModelHOUSE_ROOM.STATUS != 0)
            {
                this.comboBox7STATUS.SelectedValue = this._ModelHOUSE_ROOM.STATUS+"";
            }
            //业主
            DataSet ownerSet = this.dalHOUSE_OWNER.GetList("");
            this.comboBox8Owner.DataSource = ownerSet.Tables[0];
            this.comboBox8Owner.DisplayMember = "NAME";
            this.comboBox8Owner.ValueMember = "SEQID";
           
            if (this._ModelHOUSE_ROOM.HOUSE_OWNER_SEQID != 0)
            {
                this.comboBox8Owner.SelectedValue = this._ModelHOUSE_ROOM.HOUSE_OWNER_SEQID;
            }


            //文本信息

            this.textBox1HOUSE_NUMBER.Text = this._ModelHOUSE_ROOM.HOUSE_NUMBER;
            this.textBox1FLOOR_ORDER.Text = this._ModelHOUSE_ROOM.HOUSE_CODE+"";
            this.textBox2Note.Text = this._ModelHOUSE_ROOM.NOTE;
            this.textBox2ROOM_SIZE.Text = this._ModelHOUSE_ROOM.ROOM_SIZE+"";


              //已出租的房子不给改变状态.
            if (this._ModelHOUSE_ROOM.SEQID > 0)
            {
                if (this._ModelHOUSE_ROOM.STATUS == Constant.HOUSE_ROOM_STATUS_RENTED) {
                    this.comboBox7STATUS.Enabled = false;
                }
            
            }

        }

        private Boolean check()
        {
            try
            {
                StringUtils.checkIsDecimal(this.textBox2ROOM_SIZE.Text, "面积");
                StringUtils.checkText(this.textBox1FLOOR_ORDER.Text, 2, 30, "图号");
                StringUtils.checkText(this.textBox1HOUSE_NUMBER.Text, 2, 30, "编号");

                StringUtils.checkText(this.textBox2Note.Text, 0, 300, "备注信息");

                //更新.原值不检测
                if (this._ModelHOUSE_ROOM.SEQID > 0)
                {
                    if (_ModelHOUSE_ROOM.HOUSE_NUMBER.Equals(this.textBox1HOUSE_NUMBER.Text.Trim()))
                    {
                        return true;
                    }
                }
                String existSql = "select count(1) from HOUSE_ROOM where HOUSE_DONG_SEQID = {0} and HOUSE_NUMBER = '{1}'";
                existSql = String.Format(existSql, this._ModelHOUSE_ROOM.HOUSE_DONG_SEQID, this.textBox1HOUSE_NUMBER.Text.Trim());
                if (BllCommonUtil.Exists(existSql))
                {
                    MessageBox.Show("此名称值已存在在,请输入其它的值", "提示");
                    return false;
                }

            }

            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            return true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            }
            this._ModelHOUSE_ROOM.HOUSE_CODE = this.textBox1FLOOR_ORDER.Text+"";
            this._ModelHOUSE_ROOM.NOTE = this.textBox2Note.Text;
            this._ModelHOUSE_ROOM.ROOM_SIZE = Convert.ToDecimal(this.textBox2ROOM_SIZE.Text);
            this._ModelHOUSE_ROOM.HOUSE_NUMBER = this.textBox1HOUSE_NUMBER.Text;

            this._ModelHOUSE_ROOM.HOUSE_DONG_SEQID = Convert.ToInt32(this.comboBox2Dong.SelectedValue);
            this._ModelHOUSE_ROOM.CHAOXIANG =  (String)this.comboBox3CHAOXIANG.Text;
            this._ModelHOUSE_ROOM.ROOM_TYPE = (String)this.comboBox4ROOM_TYPE.Text;
            this._ModelHOUSE_ROOM.ROOM_FLOOR = Convert.ToInt32(this.comboBox6ROOM_FLOOR.SelectedValue);
            this._ModelHOUSE_ROOM.STATUS = Convert.ToInt32(this.comboBox7STATUS.SelectedValue);
            this._ModelHOUSE_ROOM.HOUSE_OWNER_SEQID = Convert.ToInt32(this.comboBox8Owner.SelectedValue);
          




            if (dalHOUSE_ROOM.Exists(this._ModelHOUSE_ROOM.SEQID))
            {
                dalHOUSE_ROOM.Update(this._ModelHOUSE_ROOM);
            }
            else
            {
                dalHOUSE_ROOM.Add(this._ModelHOUSE_ROOM);
            }
            //触发代理
            oneRefreshParentData(1);
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //代理
        public delegate void RefreshParentData(int page);
        public RefreshParentData oneRefreshParentData;

        private void label11_Click(object sender, EventArgs e)
        {

        }

       
        //设置楼层
        private void comboBox2Dong_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            
            int dongSeqid = 0;
            try
            {
                  dongSeqid = (int)this.comboBox2Dong.SelectedValue;
                  
            }
            catch (Exception)
            {

                return; 
            }
           

            DalHOUSE_DONG aDalHOUSE_DONG = new DalHOUSE_DONG();
            ModelHOUSE_DONG aHOUSE_DONG = aDalHOUSE_DONG.GetModel(dongSeqid);
            //楼层
            List<ComboBoxInfo> floors = new List<ComboBoxInfo>();
            if (aHOUSE_DONG != null)
            {
                for (int i = aHOUSE_DONG.ALL_FLOOR_START; i <= aHOUSE_DONG.ALL_FLOOR_END; i++)
                {
                    if (i == 0)
                    {
                        continue;
                    }
                    String floorDetail = "";
                    if (i % 5 == 0 || i == -1)
                    {
                        floorDetail = "======" + i + "=======";
                    }
                    ComboBoxInfo info = new ComboBoxInfo() { Id = i + "", Name = ""+ i + "" + floorDetail };
                    floors.Add(info);
                }
            }
          
            this.comboBox6ROOM_FLOOR.DisplayMember = "Name";
            this.comboBox6ROOM_FLOOR.ValueMember = "Id";
            this.comboBox6ROOM_FLOOR.DataSource = floors;
            if (this._ModelHOUSE_ROOM.ROOM_FLOOR != 0)
            {
                this.comboBox6ROOM_FLOOR.SelectedValue = this._ModelHOUSE_ROOM.ROOM_FLOOR + "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int dongSeqid = 0;
            try
            {
                dongSeqid = (int)this.comboBox2Dong.SelectedValue;
                if (dongSeqid <= 0) { 
                    throw new Exception("请选择排层信息");
                 }
                 StringUtils.checkText(this.textBox1HOUSE_NUMBER.Text, 2, 30, "编号");
            }
            catch (Exception ew3)
            {

                MessageBox.Show(ew3.Message, "提示");

            }
            DalHOUSE_DONG aDalHOUSE_DONG = new DalHOUSE_DONG();
            ModelHOUSE_DONG aHOUSE_DONG = aDalHOUSE_DONG.GetModel(dongSeqid);

 
            
            this._ModelHOUSE_ROOM.NOTE = this.textBox2Note.Text;
            this._ModelHOUSE_ROOM.ROOM_SIZE = Convert.ToDecimal(this.textBox2ROOM_SIZE.Text);
            this._ModelHOUSE_ROOM.HOUSE_DONG_SEQID = Convert.ToInt32(this.comboBox2Dong.SelectedValue);
            this._ModelHOUSE_ROOM.CHAOXIANG = (String)this.comboBox3CHAOXIANG.Text;
            this._ModelHOUSE_ROOM.ROOM_TYPE = (String)this.comboBox4ROOM_TYPE.Text;
            this._ModelHOUSE_ROOM.STATUS = Convert.ToInt32(this.comboBox7STATUS.SelectedValue);
            this._ModelHOUSE_ROOM.HOUSE_OWNER_SEQID = Convert.ToInt32(this.comboBox8Owner.SelectedValue);

             string header = this.textBox1HOUSE_NUMBER.Text  ;
             string tail = this.textBox1FLOOR_ORDER.Text;

            if (aHOUSE_DONG != null)
            {
                for (int i = aHOUSE_DONG.ALL_FLOOR_START; i <= aHOUSE_DONG.ALL_FLOOR_END; i++)
                {
                    if (i == 0)
                    {
                        continue;
                    }
                    String realValue = "";//i < 10 ? ("0" + i) : i + ""; //"B1-401A"; header B1-4 realValue 01 A tail
                    if (i < 10) { 
                        realValue ="00"+i ;
                    }
                    else if (i >= 10 && i < 100)
                    {
                        realValue = "0" + i;
                    }
                    else {
                        realValue = "" + i;
                    }

                    this._ModelHOUSE_ROOM.HOUSE_CODE = header + realValue + tail;
                    this._ModelHOUSE_ROOM.HOUSE_NUMBER = header + realValue + tail;
                    this._ModelHOUSE_ROOM.ROOM_FLOOR = i;

                    dalHOUSE_ROOM.Add(this._ModelHOUSE_ROOM);
                }
            }
            this.textBox1HOUSE_NUMBER.Text = "";
            MessageBox.Show("生成成功.", "提示");
        }

        

      

        
    }
}
